package com.idrem.data;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.impl.client.DefaultHttpClient;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.xmlpull.v1.XmlPullParserException;

import com.idrem.data.model.ConstantRS;
import com.idrem.data.model.DataModelType;
import com.idrem.data.model.IDataModel;
import com.idrem.data.model.KPIMessageRS;
import com.idrem.data.model.ProductRS;
import com.idrem.data.model.Response;
import com.idrem.data.model.ShopRS;
import com.idrem.data.model.TaskRS;
import com.idrem.data.model.VIPPriceRS;
import com.idrem.data.url.UrlName;

public class DataSource {

	private String TAG = "Network Data Source: ";
	
	private static final DataSource instance = new DataSource();
	
	private DataSource() {}

	/**
	 * Get an instance of this class.
	 */
	public static DataSource getInstance(){
    	return instance;
    }
	
	public Response login(HashMap<String, String> paramValue) {

		String dataStr = getData(paramValue, UrlName.LOGIN_URL);

		Response resultSet = (Response) getDataModelResult(dataStr, DataModelType.F_RESPONSE);

		return resultSet;
	}
    
	public ProductRS getProductData(HashMap<String, String> paramValue) {

		String dataStr = getData(paramValue, UrlName.PROD_URL);

		ProductRS resultSet = (ProductRS) getDataModelResult(dataStr, DataModelType.F_PRODUCT);

		return resultSet;
	}
	
	public VIPPriceRS getVIPPriceData(HashMap<String, String> paramValue) {

		String dataStr = getData(paramValue, UrlName.VIP_PRICE);

		VIPPriceRS resultSet = (VIPPriceRS) getDataModelResult(dataStr, DataModelType.F_PRODUCT);

		return resultSet;
	}
	
	
	public KPIMessageRS getKPIData(HashMap<String, String> paramValue) {

		String dataStr = getData(paramValue, UrlName.KPI_URL);

		KPIMessageRS resultSet = (KPIMessageRS) getDataModelResult(dataStr, DataModelType.F_KPI);

		return resultSet;
	}
	
	public ConstantRS getConstantData(HashMap<String, String> paramValue) {

		String dataStr = getData(paramValue, UrlName.CONSTANT_URL);

		ConstantRS resultSet = (ConstantRS) getDataModelResult(dataStr, DataModelType.F_CONSTANT);

		return resultSet;
	}
	
	public ShopRS getShopData(HashMap<String, String> paramValue) {

		String dataStr = getData(paramValue, UrlName.SHOP_URL);

		ShopRS resultSet = (ShopRS) getDataModelResult(dataStr, DataModelType.F_SHOP);

		return resultSet;
	}
	
	public TaskRS getTaskData(HashMap<String, String> paramValue) {

		String dataStr = getData(paramValue, UrlName.TASK_URL);

		TaskRS resultSet = (TaskRS) getDataModelResult(dataStr, DataModelType.F_TASK);

		return resultSet;
	}

	/**
	 * Retrieve data stream according to url.
	 * @param url
	 * @return
	 */
	private String getData(String url) {
		
		DefaultHttpClient httpClient = (DefaultHttpClient) HttpHelper.getHttpClient();
		
		int retry = 3;
		int count = 0;
		

		while(count < retry) {
			count +=1;
			try {
				String response = HttpHelper.doGet0(url, httpClient, null);
				return response;
			} catch (Exception e) {
			
				return null;
			}
		}
		return null;

	}
	
	/**
	 * Retrieve data stream according to url.
	 * @param url
	 * @return
	 */
	private String getData(HashMap<String, String> paramValue, String url) {
		
		int retry = 3;
		int count = 0;
		

		while(count < retry) {
			count +=1;
			try {
				String response = HttpHelper.postXML(paramValue, url);
				return response;
			} catch (Exception e) {
			
				return null;
			}
		}
		return null;

	}
	
	private IDataModel getDataModelResult(String dataStr, int modelType) {

		Serializer serializer = new Persister();
		
		if(dataStr == null) {
			return null;
		}
		
		try {
			if (modelType == DataModelType.F_SHOP) {
				
				return serializer.read(ShopRS.class, dataStr);
				
			} else if (modelType == DataModelType.F_RESPONSE) {
				
				return serializer.read(Response.class, dataStr);
				
			}  else {
				return null;
			}
			
		} catch (Exception e) {
			
			return null;
		}

	}
	
}
